#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QStringList list;
    list<<"姓名"<<"性别"<<"年龄";
    //设置列数
    ui->tableWidget->setColumnCount(list.size());
    //添加表头
    ui->tableWidget->setHorizontalHeaderLabels(list);
    //设置行数
    ui->tableWidget->setRowCount(5);
    //准备数据
    QStringList nameList;
    nameList<<"欧阳武"<<"潘鹏程"<<"刘翔";
    //设置正文
    ui->tableWidget->setItem(0,0,new QTableWidgetItem("李景奥"));
    ui->tableWidget->setItem(0,1,new QTableWidgetItem("男"));
    ui->tableWidget->setItem(0,2,new QTableWidgetItem("18"));
    //数据填入
    for(int i = 0 ; i < 3 ; i++){
        int col = 0 ;
        ui->tableWidget->setItem(i+1,col++,new QTableWidgetItem(nameList.at(i)));
        ui->tableWidget->setItem(i+1,col++,new QTableWidgetItem("男"));
        ui->tableWidget->setItem(i+1,col++,new QTableWidgetItem("20"));
    }
//    ui->tableWidget->itemAt()

}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_addBtn_clicked()
{
    //findItems返回值为列表,空代表不存在
    if(ui->tableWidget->findItems("李景奥",Qt::MatchExactly).empty()){
        ui->tableWidget->insertRow(0);
        ui->tableWidget->setItem(0,0,new QTableWidgetItem("李景奥"));
        ui->tableWidget->setItem(0,1,new QTableWidgetItem("男"));
        ui->tableWidget->setItem(0,2,new QTableWidgetItem("18"));
    }else{
        QMessageBox::warning(this,"警告","已存在同名数据",QMessageBox::Ok);
    }

}

void MainWindow::on_delBtn_clicked()
{if(!(ui->tableWidget->findItems("李景奥",Qt::MatchExactly).empty())){
        QTableWidgetItem * const localFirst = ui->tableWidget->findItems("李景奥",Qt::MatchExactly).first();
        ui->tableWidget->removeRow(localFirst->row());
    }else{
        QMessageBox::warning(this,"警告","不存在匹配数据",QMessageBox::Ok);
    }

}
